package com.inno.auth.domain.strategy;

import com.inno.auth.common.enums.LoginTypeEnum;
import com.inno.auth.common.enums.YesOrNoEnum;
import com.inno.auth.common.error.AuthException;
import com.inno.auth.domain.utils.Md5Utils;
import com.inno.auth.infra.basic.entity.PlannerUsers;
import com.inno.auth.infra.basic.service.PlannerUsersService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.stereotype.Component;

/**
 * 账号密码登录
 * @author sean
 */
@Slf4j
@Component
public class PasswordLoginHandler implements LoginHandler{
    @Resource
    private PlannerUsersService plannerUsersService;
    @Override
    public LoginTypeEnum getType() {
        return LoginTypeEnum.LOGIN_TYPE_PASSWORD;
    }

    @Override
    public PlannerUsers login(PlannerUsers users) {
        users.setPassword(Md5Utils.create().digestHex(users.getPassword()));
        PlannerUsers usersDB = this.plannerUsersService.queryByUsername(users);
        String isDeleted = usersDB.getIsDeleted();
        if (YesOrNoEnum.YES.getCode().equals(isDeleted)){
            throw AuthException.of("账号可能已注销");
        }
        if (!usersDB.getPassword().equals(users.getPassword())){
            throw AuthException.of("登录失败 登录信息填写有误");
        }
        return usersDB;
    }
}
